package com.boc.image.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.boc.bus.control.BusOutputResult;
import com.boc.ibatis.config.IbatisSqlClient;

public class UserTask {
	
	/**
	 * 根据用户ID取用户当前可接收的任务列表
	 * @return
	 */
	public List getTask(Object userId){
		List result = null;
		try {
			result = IbatisSqlClient.sqlMapClient.queryForList("image_t_img_task.getUserTask",userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 用户接收任务
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult receviTask(HttpServletRequest request,HttpServletResponse response,Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			/**
			 * 确认该用户是否可以接收此任务
			 */
			m.put("nUserid", request.getSession().getAttribute("userId"));
			Object zt = IbatisSqlClient.sqlMapClient.queryForObject("image_t_img_task.getTaskZt",m);
			result.addValue("C_ZT", zt);
			if(zt == null) {
				IbatisSqlClient.sqlMapClient.insert("image_t_img_task.insertUserTask",m);
			}
			result.addValue("PN_RET", "0");
			result.addValue("PC_MSG", "");
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 取用户历史任务
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public List getPageContent(Map param) {
		List result = null;
		int currPage = 0;
		int pageRows = 0;
		try {
			currPage = Integer.parseInt(param.get("currPage").toString());
			pageRows = Integer.parseInt(param.get("pageRows").toString());
			result = IbatisSqlClient.sqlMapClient.queryForList(
					"image_t_img_task.getUserHistoryTask", param,
					(currPage - 1) * pageRows, pageRows);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 取分页参数
	 * @return
	 */
	public Map geSumPages(Map parameter){
		Map result = new HashMap();
		int pageRows = 20;//每页显示多少行
		int totalPages = 0;//总页数
		int totalRows = 0;//总行数
		try {
			Object rows = IbatisSqlClient.sqlMapClient.queryForObject("image_t_img_task.getTaskRows",parameter);
			totalRows = Integer.parseInt(rows.toString());
			totalPages = totalRows % pageRows == 0? (totalRows / pageRows):(totalRows / pageRows)+1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("totalRows", new Integer(totalRows));
		result.put("totalPages", new Integer(totalPages));
		result.put("pageRows", new Integer(pageRows));
		
		return result;
	}
	
	/**
	 * 提交帖子任务
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult commitTask(HttpServletRequest request,HttpServletResponse response,Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			m.put("nUserid", request.getSession().getAttribute("userId"));
			IbatisSqlClient.sqlMapClient.insert("image_procedure.userTaskTz", m);
			result.addValue("PN_RET", m.get("PN_RET"));
			result.addValue("PC_MSG", m.get("PC_MSG"));
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 根据任务接收编号取用户帖子任务信息
	 * @param nId(任务接收编号)
	 * @return
	 */
	public Map getUserTzInfo(Object nId){
		Map result = null;
		try {
			result = (Map) IbatisSqlClient.sqlMapClient.queryForObject("image_t_img_tztask.getTaskInfo",nId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
